
import msg from '../components/Message'
module.exports = function(list){
  if(!list.length) return ;
  var bl = true;
  var str = '';
  for(var i=0;i<list.length;i++){
    var rec   = list[i],
      text  = rec['text'],
      type  = rec['type'],
      data  = rec['data'];
    switch(type){
      case 'tel':
        var re = /^1\d{10}$/;
        if (!re.test(data)) {
          str = text || "请输入正确的手机号码";
          bl = false;
          break;
          };
        break;
      case 'wechat':
        var re = /^(([a-zA-Z])[a-zA-Z0-9_-]{5,19})+$/;
        if(!re.test(data)){
          str = text;
          bl = false;
        }
        break;
      case 'empty':
        if(data===''){
          str = text;
          bl = false;
        }
        break;
      case 'password':
        if(data != rec['other']){
          str = text;
          bl = false;
        }
        break;
      case "length":
        if(data.length > rec['other']){
          str = text;
          bl = false;
        }
        break;
      case "idcrad":
        // var re = /^\d+$/;
        // if(!re.test(data)){
        //  str='请输入正确的身份证号';
        //  bl = false;
        //  break;
        // }
        if(data.length!=15 && data.length!=18){
          str = '请输入正确的身份证号'
          bl = false;
          break;
        }
        
        break;
      case 'exec':
        if(!rec.other.test(data)){
          str = text;
          bl = false;
          break; 
        }
        break;
      case 'qq':
        var re = /^[1-9]\d{4,11}$/;
        if(!re.test(data)){
          str = text;
          bl = false;
        }
        break; 
      case 'numLetter':   //只能输入数字和英文
        var re = /^[0-9a-zA-Z-_]*$/
        if(!re.test(data)){
          str = text;
          bl = false;
        }
        break; 
      case 'isTriDecimal':
        var length=rec['other']==undefined?2:rec['other']
        data=data.toString()
        var decimalIndex = data.indexOf('.');
        if (decimalIndex == '-1') {
            bl=true
        } else {
            var decimalPart = data.substring(decimalIndex + 1, data.length);
            if (decimalPart.length <= parseInt(length)) {
                bl= true;
            } else {
                str = text;
                bl= false;
            }
        }
        break;
      case 'ChNumLetter':   //只能输入数字,英文和中文
          if(data=='') {
            bl=true;
            break;
          }
          var re = /^[\u4E00-\u9FA5A-Za-z0-9]+$/
          if(!re.test(data)){
            str = text;
            bl = false;
          }
          break; 
      case "minLength":
        if(data.length < rec['other']){
          str = text;
          bl = false;
        }
        break;    
        
    }
    if(!bl) break;
  }
  if(!bl) msg.info(str)
  return bl;
}
    
